import React, { Component } from 'react'
import { NavigationActions } from 'react-navigation'
import DynamicTabNavigator from '../navigator/DynamicTabNavigator'
import NavigationUtil from '../navigator/NavigationUtil'
import BackPressComponent from '../common/BackPressComponent'

export default class HomePage extends Component {
	constructor(props) {
		super(props)
		//处理Android中的物理返回键
		this.backPress = new BackPressComponent({
			backPress: this.onBackPress()
		})
	}

	componentDidMount() {
		this.backPress.componentDidMount()
	}

	componentWillUnmount() {
		this.backPress.componentWillUnmount()
	}

	onBackPress = () => {
		const { dispatch, nav } = this.props
		//如果RootNavigator中的MainNavigator的index为0，则不处理返回事件
		if (nav && nav.routes[1].index === 0) {
			return false
		}
		dispatch && dispatch(NavigationActions.back())
		return true
	}

	render() {
		//这里将this.props.navigation暂存，方便子路由可以跳转到父路由同级路由
		NavigationUtil.navigation = this.props.navigation
		return <DynamicTabNavigator />
	}
}
